package com.nuoniu.sibanyun.controller.erp;


import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.erp.ImplementInfo;
import com.nuoniu.sibanyun.service.erp.IImplementInfoService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 贯标实施书 前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2021-01-06
 */
@RestController
@RequestMapping("/erp/implement")
@Api(value = "贯标实施", tags = "贯标实施")
public class ImplementInfoController extends BaseController {

    @Autowired
    private IImplementInfoService implementInfoService;

    @ApiOperation("获取列表")
    @GetMapping("getList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型"),
    })
    public HttpResult getList(Integer type) {
        return HttpResult.ok(implementInfoService.getList(type));
    }

    @ApiOperation("获取详情")
    @GetMapping("getDetail")
    public HttpResult getDetail(Long id) {
        return HttpResult.ok(implementInfoService.getById(id));
    }

    @ApiOperation("保存")
    @PostMapping("save")
    public HttpResult save(@RequestBody ImplementInfo implementInfo) {
        if(implementInfo.getType() == null) {
            throw new NuoNiuException("类型不能为空");
        }

        if(StringUtils.isBlank(implementInfo.getTitle())) {
            throw new NuoNiuException("标题不能为空");
        }

        if(StringUtils.isBlank(implementInfo.getPlanDate())) {
            throw new NuoNiuException("计划完成时间不能为空");
        }

        UserInfo userInfo = UserThreadLocal.get();
        if(implementInfo.getId() == null) {
            implementInfo.setCompanyId(userInfo.getCompanyId());
            implementInfo.setCreateUserId(implementInfo.getUpdateUserId());
        }else {
            ImplementInfo info = implementInfoService.getById(implementInfo.getId());
            if(info != null && info.getStatus() == 1) {
                throw new NuoNiuException("已审核,不能修改");
            }
            implementInfo.setUpdateDate(LocalDateTime.now());
            implementInfo.setUpdateUserId(userInfo.getUserId());
        }
        implementInfoService.saveOrUpdate(implementInfo);
        return HttpResult.ok();
    }

    @GetMapping("examine")
    @ApiOperation("审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id"),
            @ApiImplicitParam(name = "status", value = "状态 0:审核,1:已审")
    })
    public HttpResult examine(Long id, Integer status) {
        ImplementInfo implementInfo = new ImplementInfo();
        implementInfo.setId(id);
        implementInfo.setStatus(status);
        implementInfoService.updateById(implementInfo);
        return HttpResult.ok();
    }

    @ApiOperation("删除")
    @DeleteMapping("delete")
    public HttpResult delete(Long id) {
        ImplementInfo implementInfo = implementInfoService.getById(id);
        if(implementInfo != null && implementInfo.getStatus() == 1) {
            throw new NuoNiuException("已审核,不能删除");
        }

        implementInfoService.removeById(id);
        return HttpResult.ok();
    }
}
